package com.controller;

import org.springframework.web.bind.annotation.RestController;
import com.entity.User;
import com.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@CrossOrigin(origins = "http://127.0.0.1:5500/")
@RestController
@RequestMapping("/api")
public class UserController {

    @Autowired
    UserRepository userRepository;

    //查询所有  根据名字查询
    @GetMapping("/users")
    public ResponseEntity<List<User>> findAllUsers(@RequestParam(required = false)String name){
        try{
            List<User> users = new ArrayList<User>();

            if(name == null){
                userRepository.findAll().forEach(users::add);
            }else{
                userRepository.findByNameContaining(name).forEach(users::add);
            }

            if(users.isEmpty()){
                return new ResponseEntity<>(HttpStatus.NO_CONTENT);
            }
            return new ResponseEntity<>(users,HttpStatus.OK);
        }catch (Exception e){
            return new ResponseEntity<>(null,HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    //根据id查询
    @GetMapping("/user/{id}")
    public ResponseEntity<User> findById(@PathVariable("id")Integer id){
        Optional<User> user = userRepository.findById(id);

        if(!user.isPresent()){
            return new ResponseEntity<>(null,HttpStatus.NOT_FOUND);
        }else{
            return new ResponseEntity<>(user.get(),HttpStatus.OK);
        }

    }

    //新增
    @PostMapping("/user")
    public ResponseEntity<User> createUser(@RequestBody User user){
        try{
            User user1 = userRepository
                    .save(new User(user.getName(),user.getPassword(),user.getAge(),user.getAddress()));
            return new ResponseEntity<>(user1,HttpStatus.CREATED);
        }catch (Exception e){
            return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    //根据id修改
    @PutMapping("/user/{id}")
    public ResponseEntity<User> updateUser(@PathVariable("id")Integer id, @RequestBody User user){
        Optional<User> user1 = userRepository.findById(id);
        if(user1.isPresent()){
            User user2 = user1.get();
            user2.setAddress(user.getAddress());
            user2.setAge(user.getAge());
            user2.setName(user.getName());
            user2.setPassword(user.getPassword());
            return new ResponseEntity<>(userRepository.save(user2),HttpStatus.OK);
        }else {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }

    //根据id删除
    @DeleteMapping("/user/{id}")
    public ResponseEntity<User> deleteById(@PathVariable("id")Integer id){
        try{
            userRepository.deleteById(id);
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }catch (Exception e){
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    //删除所有
    @DeleteMapping("/users")
    public ResponseEntity<User> deleteAll(){
        try{
            userRepository.deleteAll();
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }catch (Exception e){
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }


}
